package com.booth.base.controllers;

import javax.servlet.http.HttpSession;

import com.booth.base.model.GroupsModules;
import com.booth.base.model.ModuleActions;
import com.booth.base.model.Users;
import com.booth.base.render.JCaptchaRender;
import com.booth.base.utils.ResultStatus;
import com.jfinal.aop.ClearInterceptor;
import com.jfinal.aop.ClearLayer;

/***
 * 主界面控制器
 * 
 * @TypeName：MainController @description：
 * @author：Administrator
 */
public class MainController extends BaseController {
	/***
	 * 登录界面
	 */
	@ClearInterceptor(ClearLayer.ALL) //不拦截
	public void index() {
		render("index.jsp");
	}

	/***
	 * 进入主界面
	 */
	public void main() {
		String name = getLoginUserName();
		if (name != null && !name.equals("")) {
			setAttr("userName", name);
			render("main.jsp");
		} else {
			render("index.jsp");
		}

	}
	
	public void home() {
		render("home.jsp");
	}

	/***
	 * 登录验证
	 */
	@ClearInterceptor(ClearLayer.ALL)
	public void doLogin() {
		String saveCode = null;
		HttpSession session = getSession();
		Object obj = session.getAttribute("code");
		if (obj != null) {
			saveCode = ((String) obj).toLowerCase();
		}
		String code = getPara("code");
		String userName = getPara("userName");
		String userPwd = getPara("userPwd");

		if ((code != null) && (!"".equals(code)) && (code.toLowerCase().equals(saveCode))) {
			Users user = Users.doLogin(userName, userPwd);
			if (user != null) {
				user.setUserId(user.getInt("user_id"));
				user.setUserName(user.getStr("user_name"));
				user.setGroupType(user.getInt("group_type"));
				user.setGroupName(user.getStr("group_name"));
				user.setActionUrls(ModuleActions.findByGroupId(user.getInt("group_id").intValue()));
				user.setModuleIds(GroupsModules.findByGroupsId(user.getInt("group_id").intValue()));
				session.setAttribute("user", user);
				renderResult(ResultStatus.SUCCESS);
			} else {
				renderResult(ResultStatus.BAD_USERNAME_OR_PASSWORD);
			}
		} else {
			renderResult(ResultStatus.BAD_CODE);
		}
	}

	/***
	 * 生成二维码
	 */
	@ClearInterceptor(ClearLayer.ALL)
	public void random_code() {
		render(new JCaptchaRender());
	}

	/***
	 * 登出
	 */
	@ClearInterceptor(ClearLayer.ALL)
	public void logout() {
		HttpSession session = getSession();
		session.invalidate();
		renderResult(ResultStatus.SUCCESS);
	}
}